﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodSalesLimit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "BalanceCenter/GoodSalesLimit.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = DAL.Views.V_SalesLimit.Select("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            TextBox tb1 = (TextBox)e.Item.Cells[4].FindControl("tbSalesMoney");
            TextBox tb2 = (TextBox)e.Item.Cells[5].FindControl("tbSalesDay");

            tb1.Text = dr["SalesMoney"].ToString();
            tb2.Text = dr["SalesDay"].ToString();
        }
    }

    protected void g_ItemCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            if (e.CommandName == "btnGO")
            {
                long ID = ShoveEL._Convert.StrToLong(e.Item.Cells[8].Text, 0);

                TextBox tb1 = (TextBox)e.Item.Cells[4].FindControl("tbSalesMoney");
                TextBox tb2 = (TextBox)e.Item.Cells[5].FindControl("tbSalesDay");

                double SalesMoney = ShoveEL._Convert.StrToDouble(tb1.Text.Trim(), 0);

                if (SalesMoney <= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "请填入正确的可消费总额!");

                    return;
                }

                int SalesDay = ShoveEL._Convert.StrToInt(tb2.Text.Trim(), 0);

                if (SalesDay <= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this.Page, "请填入正确的消费时间!");

                    return;
                }

                try
                {
                    int i = DAL.Tables.T_SalesLimit.Update("[ID]=" + ID, 1, false, 1, false, SalesMoney, true, SalesDay, true,0,false,DateTime.Now,false);
                    ShoveEL.Database.MSSQL.ExecuteSQLScript("update T_Users set SalesDay = '" + DateTime.Now + "' where ID=" + ID);

                    if (i >= 0)
                    {
                        ShoveEL._Web.JavaScript.Alert(this.Page, "修改成功!");

                        return;
                    }
                }
                catch { }
            }
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (tbUserNo.Text.Trim().Equals(""))
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "会员编号不能为空！");

            return;
        }

        int ReturnValue = 0;
        string ReturnDescription = "";

        try
        {
            DAL.Procedures.P_AddSalesLimit.Call(tbUserNo.Text.Trim(), ref ReturnValue, ref ReturnDescription);

            if (ReturnValue < 0)
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, ReturnDescription);

                return;
            }
        }
        catch { }

        BindData();        
    }
}
